import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入

@Component({
	selector: 'sa-role',
	templateUrl: './role.component.html',
})
export class RoleComponent implements OnInit {
	tableData = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	btnData:any = [];
	roleAdd:boolean = false;
	roleEdit:boolean = false;
	roleDeleteBatch:boolean = false;
	roleMenupermission:boolean = false;
	roleDatapermission: boolean = false;
	roleHomepermission:boolean = false; //首页权限按钮
	userType: string = ''; //用户类型
	userTypeList:any = []; // 用户类型列表
	constructor(private jsonApiService: JsonApiService, private router: Router,private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage || 1;
		});
	}
	
	ngOnInit() {
		this.load();
		this.initBtn();
		this.initUserTypeList();
	}
	//初始化权限按钮
	initBtn(){
		this.btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_system:role')));
		if(!this.btnData){return};
		for(let item of this.btnData){
			if(item.sourceKey === "role:add"&&item.parentId){this.roleAdd = true;}
			if(item.sourceKey === "role:edit"&&item.parentId){this.roleEdit = true;}
			if(item.sourceKey === "role:deleteBatch"&&item.parentId){this.roleDeleteBatch = true;}
			if(item.sourceKey === "role:menupermission"&&item.parentId){this.roleMenupermission = true;}
			if(item.sourceKey === "role:datapermission"&&item.parentId){this.roleDatapermission = true;}
			if(item.sourceKey === "role:homepermission"&&item.parentId){this.roleHomepermission = true;}
		}
	}
	//表格搜索
	table_search() {
		this.curPage = 1
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//刷新
	refresh(){
    	this.load();
    }
	//初始化加载list数据
	load() {
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				userName:this.searchKey,
				userType: this.userType
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("role/list.html", reqdata).subscribe(data => {
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
		})
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//新建
	new_role(){
		this.router.navigate(['/system/role/role-page'], {queryParams: {type:'new'}});
	}
	//编辑页面
	updata_role(row) {
		this.router.navigate(['/system/role/role-page'], {queryParams: {id: row.id,type: 'updata',nowPage:this.curPage}});
	}
	//删除
	del_role(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			let reqdata = 'ids=' + row.id;
			this.jsonApiService.http("role/deleteBatch.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.load();
			})
		})
	}
	//首页权限分配
	home_access(data){
		this.router.navigate(['/system/role/role-authorize'],{ queryParams: { id: data.id,type:'首页',roleName:data.name,nowPage:this.curPage}});
	}
	//菜单权限分配
	menu_access(data){
		this.router.navigate(['/system/role/role-authorize'],{ queryParams: { id: data.id,type:'菜单',roleName:data.name,nowPage:this.curPage}});
	}
	//车辆权限分配
	car_access(data){
		this.router.navigate(['/system/role/role-authorize'],{ queryParams: { id: data.id,type:'车辆',roleName:data.name,nowPage:this.curPage}});
	}
	//围栏权限分配
	rail_access(data){
		this.router.navigate(['/system/role/role-authorize'],{ queryParams: { id: data.id,type:'围栏',roleName:data.name,nowPage:this.curPage}});
	}
	//绑定,车辆自动绑定公司
	binding(data){
		this.router.navigate(['/system/role/role-authorize'],{ queryParams: { id: data.id,type:'绑定',roleName:data.name,nowPage:this.curPage}});
	}

	// 获取用户类型列表
	initUserTypeList(){
		this.jsonApiService.http("role/userTypeList.html",{}).subscribe(data=> {
			if(data.success){
				for (let index = 0; index < data.data.length; index++) {
					const element = data.data[index];
					element.text = element.userTypeName
					element.id = element.userType
				}
				this.userTypeList = data.data
			}
		})
	}
}